package com.example.myapplication.View

import android.content.Context
import android.os.Parcel
import android.os.Parcelable
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.LifecycleOwner

class MyChronometer(context: Context?, attrs: AttributeSet?) : Chronometer(context, attrs), LifecycleEventObserver {
    var elapsedTime: Long = 0
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when(event){
            Lifecycle.Event.ON_RESUME ->{
                this.base = SystemClock.elapsedRealtime() - elapsedTime
                this.start()
            }
            Lifecycle.Event.ON_PAUSE ->{
                elapsedTime = SystemClock.elapsedRealtime() - this.base

                this.stop()
            }
            else ->{

            }
        }
    }
}